package com.guldan.thrall.web.controller;

import com.guldan.core.controller.BaseController;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.response.ResponseJson;
import com.guldan.jaina.dto.datastat.datav.DatavDTO;
import com.guldan.jaina.dto.datastat.datav.MetaDTO;
import com.guldan.jaina.dto.report.Point;
import com.guldan.jaina.dto.report.Points;
import com.guldan.jaina.provider.IReportProvider;
import com.guldan.thrall.dto.report.DVDTO;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Silas.
 * Date: 2017/3/5
 * Time: 12:32
 */

@Controller
@RequestMapping("/datav")
public class DatavController extends BaseController {

    @Autowired
    private IReportProvider iReportProvider;

    //post
    @RequestMapping(value = "/area", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    DVDTO area(@RequestBody DVDTO dto) {

        DVDTO dvdto = create(dto.getDivisionCode());
        logger.info(JSONObject.fromObject(dvdto).toString());
        return dvdto;
    }

    //post
    @RequestMapping(value = "/area2", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    DVDTO area2(@RequestBody DVDTO dto) {

        DVDTO dvdto = create2(dto.getDivisionCode());
        logger.info(JSONObject.fromObject(dvdto).toString());
        return dvdto;
    }


    public DVDTO create(String divisionCode) {
        ResponseDTO<DatavDTO> response = iReportProvider.getDatavDTO(divisionCode);

        DatavDTO oriDatavDTO = response.getData();
        DVDTO dvdto = new DVDTO();

        dvdto.setAreaId(oriDatavDTO.getAreaId());
        dvdto.setBegin(oriDatavDTO.getBegin());
        dvdto.setEnd(oriDatavDTO.getEnd());
        dvdto.setGrantTotal(oriDatavDTO.getGrantTotal());
        dvdto.setGrantMoney(oriDatavDTO.getGrantMoney());

        //项目关键词
        List<Points> pointsList01 = new ArrayList<>();
        Map<Integer, String> map = new HashMap<>();
        for (MetaDTO metaDTO : oriDatavDTO.getProjectRank()) {
            if (!map.containsKey(metaDTO.getProjectCategoryId())) {
                Points points = new Points();
                points.setName(metaDTO.getProjectCategoryName());
                Integer d = 0;
                List<Point> pointList = new ArrayList<>();
                for (MetaDTO m : oriDatavDTO.getProjectRank()) {
                    if (m.getProjectCategoryId().equals(metaDTO.getProjectCategoryId())) {
                        d += m.getGrantTotal();
                        pointList.add(new Point(m.getProjectStr(), m.getGrantTotal().doubleValue()));
                    }
                }
                points.setY(d.doubleValue());
                points.setPointList(pointList);
                map.put(metaDTO.getProjectCategoryId(), metaDTO.getProjectCategoryName());
                pointsList01.add(points);
            }
        }

/*
        Points points11 = new Points();
        List<Point> pointList11 = oriDatavDTO.getProjectRank().stream().map(metaDTO -> new Point(metaDTO.getProjectStr(), metaDTO.getGrantTotal().doubleValue())).collect(Collectors.toList());
        points11.setName("补贴项目发放关键词");
        points11.setPointList(pointList11);
        pointsList01.add(points11);*/

        dvdto.setProjectRank(pointsList01);

        //地区发放情况
        dvdto.setAreaData(oriDatavDTO.getAreaData());

        //受益年龄
        List<Points> pointsList02 = new ArrayList<>();
        Points points21 = new Points();
        List<Point> pointList21 = oriDatavDTO.getAgeData().stream().map(metaDTO -> new Point(metaDTO.getAgeStr(), metaDTO.getGrantTotal().doubleValue())).collect(Collectors.toList());
        points21.setName("受益人群年龄占比");
        points21.setPointList(pointList21);
        pointsList02.add(points21);
        dvdto.setAgeData(pointsList02);

        //当前按月统计发放情况
        List<Points> pointsList03 = new ArrayList<>();
        Points points31 = new Points(), points32 = new Points();
        List<Point> pointList31 = new ArrayList<>(), pointList32 = new ArrayList<>();
        for (MetaDTO metaDTO : oriDatavDTO.getMonthData()) {
            pointList31.add(new Point(metaDTO.getDateFlag() + "月", metaDTO.getGrantTotal().doubleValue()));
            pointList32.add(new Point(metaDTO.getDateFlag() + "月", metaDTO.getGrantMoney().doubleValue()));

        }
        points31.setName("人次");
        points31.setPointList(pointList31);

        points32.setName("金额");
        points32.setPointList(pointList32);

        pointsList03.add(points31);
        pointsList03.add(points32);

        dvdto.setMonthData(pointsList03);

        //受益人群性别占比
        List<Points> pointsList04 = new ArrayList<>();
        Points points41 = new Points();
        List<Point> pointList41 = oriDatavDTO.getGenderData().stream().map(metaDTO -> new Point(metaDTO.getGenderStr(), metaDTO.getGrantTotal().doubleValue())).collect(Collectors.toList());
        points41.setName("受益人群性别占比");
        points41.setPointList(pointList41);
        pointsList04.add(points41);
        dvdto.setGenderData(pointsList04);

        return dvdto;
    }

    public DVDTO create2(String divisionCode) {
        ResponseDTO<DatavDTO> response = iReportProvider.getDatavDTO(divisionCode);

        DatavDTO oriDatavDTO = response.getData();
        DVDTO dvdto = new DVDTO();

        dvdto.setAreaId(oriDatavDTO.getAreaId());
        dvdto.setBegin(oriDatavDTO.getBegin());
        dvdto.setEnd(oriDatavDTO.getEnd());
        dvdto.setGrantTotal(oriDatavDTO.getGrantTotal());
        dvdto.setGrantMoney(oriDatavDTO.getGrantMoney());

        //近x年地区发放金额，按年汇总
        dvdto.setAreaYearData(oriDatavDTO.getAreaYearData());

        //地区发放情况
        dvdto.setAreaData(oriDatavDTO.getAreaData());

        //近x年资金类别占比
        dvdto.setCategoryYearData(oriDatavDTO.getCategoryYearData());

        return dvdto;
    }
}
